package com.lib.krismile.base.adapter;

import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

import com.chad.library.adapter.base.BaseQuickAdapter;

import org.jetbrains.annotations.NotNull;
import java.util.List;

/*
 * @author ： wangJiaWei
 * @date : 2021/3/31  15:59
 * @description ：
 */
public class BaseCommonAdapter<T> extends BaseQuickAdapter<T,BaseCommonViewHolder> {

    /**
     * 实体类中绑定的 variable 中的 name
     */
    private final int variableId;

    /**
     * layout 资源文件
     */
    private int layoutResId;

    public BaseCommonAdapter(int layoutResId,int variableId) {
        super(layoutResId);
        this.layoutResId = layoutResId;
        this.variableId = variableId;
    }

    public BaseCommonAdapter(int layoutResId, List<T> list, int variableId) {
        super(layoutResId,list);
        this.variableId = variableId;
    }


    @NotNull
    @Override
    public BaseCommonViewHolder onCreateViewHolder(@NotNull ViewGroup parent, int viewType) {
        ViewDataBinding dataBinding = DataBindingUtil.inflate(LayoutInflater.from(getContext()),this.layoutResId,parent, false);
        BaseCommonViewHolder baseCommonViewHolder = new BaseCommonViewHolder(dataBinding);
        //这里必须绑定 一下CLickListener 否则导致点击事件不起效果
        bindViewClickListener(baseCommonViewHolder,viewType);
        return baseCommonViewHolder;
    }

    @Override
    protected void convert(@NotNull BaseCommonViewHolder baseHolder, T entity) {
        baseHolder.binding.setVariable(variableId,entity);
        //防止数据闪烁
        baseHolder.binding.executePendingBindings();

    }
}
